package io.gitee.zhucan123.core.config;

import org.springframework.format.Formatter;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

@Component
public class LocalConverter implements Formatter<LocalDateTime> {

  private static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

  @Override
  @NonNull
  public LocalDateTime parse(@NonNull String s, @NonNull Locale locale) throws ParseException {
    return LocalDateTime.from(DateTimeFormatter.ofPattern(PATTERN, locale).parse(s));
  }

  @Override
  @NonNull
  public String print(@NonNull LocalDateTime localDateTime, @NonNull Locale locale) {
    return DateTimeFormatter.ofPattern(PATTERN, locale).format(localDateTime);
  }
}
